package me.jianping.oa.controller;

import me.jianping.oa.pojo.TbUser;
import me.jianping.oa.utils.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class ViewController {

    @RequestMapping(value = {"/","index"})
    public String showIndex(Map<String,Object> map){

        map.put("userName","system");
        map.put("userImg","img/head_sculpture.png");
        map.put("userId",1);
        map.put("userDepartment","市场部");

        return "index";
    }

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String loginGet(Map<String,Object> map, HttpServletRequest request){
        String path =  "http://" + request.getServerName() //服务器地址
                + ":"
                + request.getServerPort();//端口号;

        map.put("path",path);
        System.out.println("1111111111111111111111");
        return "login";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST )
    @ResponseBody
    public ResultData loginpOst(Map<String,Object> map, @RequestBody TbUser user, HttpSession httpSession){
        ResultData resultData = new ResultData();
        System.out.println("222222222222");
        if (user.getUserName().equals("system") && user.getPassword().equals("123456")){
            //写入session
            httpSession.setAttribute("USER_SESSION",user.getUserName());

            resultData.setStatus("y");

            return resultData;
        }

        resultData.setStatus("n");
        return resultData;
    }
}
